package com.tbb.app.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.tbb.app.base.dto.NoteAddDTO;
import com.tbb.app.base.service.IChannelService;
import com.tbb.app.base.service.IFileService;
import com.tbb.app.base.service.INoteService;
import com.tbb.app.base.vo.ChannelVO;
import com.tbb.app.base.vo.NoteVO;
import com.tbb.app.common.entity.BaseResponse;
import com.tbb.app.common.entity.PageBeanSearch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/api/hyc")
@Slf4j
@ApiSupport(order = 1)
@Api(tags = "何钇岑相关接口")
@Validated
//@CrossOrigin(origins = "*")
public class HYCController {

    @Autowired
    private IFileService iFileService;
    @Autowired
    private IChannelService iChannelService;
    @Autowired
    private INoteService iNoteService;

    @PostMapping("/file/upload")
    @ApiOperation("文件上传")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "file", value = "单文件上传", required = true, dataType = "MultipartFile", allowMultiple = true, paramType = "form")
    public BaseResponse<String> uploadFile(
            @RequestPart("file")
            @NotNull(message = "文件不能为空")
                    MultipartFile file) {
        return BaseResponse.data(iFileService.upload(file));
    }

    @GetMapping("/channel/list")
    @ApiOperation("列表查询频道")
    @ApiOperationSupport(order = 2)
    public BaseResponse<List<ChannelVO>> listChannel() {
        return BaseResponse.data(iChannelService.listVO());
    }

    @PostMapping("/note/publish")
    @ApiOperation("发布笔记或修改已发布笔记")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Object> publishNote(
            @RequestBody
            @Validated
                    NoteAddDTO dto) {
        iNoteService.add(dto, true);
        return BaseResponse.ok();
    }

    @GetMapping("/note/delete")
    @ApiOperation("删除本人笔记")
    @ApiOperationSupport(order = 4)
    public BaseResponse<Object> deleteNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String noteId) {
        iNoteService.removeNote(noteId);
        return BaseResponse.ok();
    }

    @GetMapping("/note/detail")
    @ApiOperation("查询本人笔记详情")
    @ApiOperationSupport(order = 5)
    public BaseResponse<NoteVO> detailNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String noteId) {
        return BaseResponse.data(iNoteService.detail(noteId, true));
    }

    @PostMapping("/note/draft/add")
    @ApiOperation("保存笔记草稿")
    @ApiOperationSupport(order = 6)
    public BaseResponse<Object> addDraft(
            @RequestBody
            @Validated
                    NoteAddDTO dto) {
        iNoteService.add(dto, false);
        return BaseResponse.ok();
    }

    @PostMapping("/note/draft/page")
    @ApiOperation("分页查询笔记草稿")
    @ApiOperationSupport(order = 7)
    public BaseResponse<IPage<NoteVO>> pageDraft(
            @RequestBody
                    PageBeanSearch search) {
        return BaseResponse.data(iNoteService.pageDraft(search));
    }

}
